<?php

namespace App\Http\Controllers\ClubLeader;

use App\Club;
use App\ClubCoaches;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ClubCoachController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $club_id = $request->session()->get('club_id');
        $ccoaches = ClubCoaches::where('club_id',$club_id)->Paginate(8);
        return view('ClubLeader.clubCoach', compact('ccoaches'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('ClubLeader.createClubCoach');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $club = Club::latest('created_at')->first();
        $club_id = $club -> value('id');

        $rules = [
            'coach_name' => 'required',
            'coach_intro' => 'required|min:6',
            'coach_avatar' => 'required'
        ];

        $this -> validate($request, $rules);
        $this -> uploadAvatar($request);
        $clubCoachAvatar = $request -> get('coach_name')
            . '.' . $request -> file('coach_avatar')
                -> getClientOriginalExtension();

        $clubCoach = new ClubCoaches;
        $clubCoach -> coach_avatar = $clubCoachAvatar;
        $clubCoach -> coach_name = $request -> get('coach_name');
        $clubCoach -> coach_intro = $request -> get('coach_intro');
        $clubCoach -> club_id = $club_id;

        $clubCoach -> save();

        $coach_count = $club -> value('coach_count');
        $club -> coach_count = $coach_count + 1;
        $club -> save();

        session() -> flash('success', '添加俱乐部教练信息成功');

        return redirect() -> route('clubCoach.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $clubCoach = ClubCoaches::findOrFail($id);
        return view('ClubLeader.editClubCoach', compact('clubCoach'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $clubCoach = ClubCoaches::findOrFail($id);

        if ($request -> file('coach_avatar'))
        {
            $avatar = $clubCoach -> coach_avatar;
            if ($avatar != 'default.jpg')
            {
                Storage::disk('clubs_coach') -> exists($avatar);
                Storage::disk('clubs_coach') -> delete($avatar);
            }
            $this -> uploadAvatar($request);
            $clubCoach -> coach_avatar = $request -> get('coach_name')
            . '.' . $request -> file('coach_avatar')
                -> getClientOriginalExtension();
            $clubCoach -> save();
        }

        $clubCoach -> update([
            'coach_name' => $request -> get('coach_name'),
            'coach_intro' => $request -> get('coach_intro')
        ]);
        $clubCoach -> save();

        session() -> flash('success', '更新成功');

        return redirect() -> route('clubCoach.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $club = Club::latest('created_at')->first();
        $clubCoach = ClubCoaches::findOrFail($id);

        $avatar = $clubCoach -> coach_avatar;
        if ($avatar != 'default.jpg')
        {
            Storage::disk('clubs_coach') -> exists($avatar);
            Storage::disk('clubs_coach') -> delete($avatar);
        }
        $clubCoach -> delete();

        $coach_count = $club -> value('coach_count');
        $club -> coach_count = $coach_count - 1;
        $club -> save();

        session() -> flash('success', '删除成功');

        return redirect() -> route('clubCoach.index');
    }

    /**
    * Upload Pic to public_path
    * @param Request $request
    */
    public function uploadAvatar(Request $request)
    {
        if ($request->isMethod('post') OR $request->isMethod('patch')) {

            $avatar = $request->file('coach_avatar');

            if ($avatar->isValid()) {

                $realPath = $avatar->getRealPath();
                $ext = $avatar->getClientOriginalExtension();
                $clubName = $request->get('coach_name');
                $fileName = $request->get('coach_name'). '.' .$ext;
                Image::make($avatar)->fit(300, 300)->save(public_path('/uploads/clubs_coach/avatars/') . $fileName);
            }
        }
    }
}
